package com.yonyou.iuap.shell.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.yonyou.iuap.pub.DateUtil;
import com.yonyou.iuap.pub.IDFactory;
import com.yonyou.iuap.shell.dao.KmSettingEntityDao;
import com.yonyou.iuap.shell.entity.KmSettingEntity;
import com.yonyou.iuap.shell.entity.exp.KmSettingQueryPoJo;
import com.yonyou.iuap.shell.entity.exp.UserIndustryPojo;
import com.yonyou.iuap.shell.pub.IShellDaoEnum;
import com.yonyou.iuap.shell.pub.IShellServiceEnum;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IKmSettingService;
@Service(value=IShellServiceEnum.Shell_Service_Setting)
public class KmSettingServiceImpl implements IKmSettingService {
	@Autowired
	@Qualifier(IShellDaoEnum.Shell_Dao_Setting)
	private KmSettingEntityDao kmSettingEntityDao;
	
	
	public ResultDto querySettingByUser(RequestDto reqDto) {
		ResultDto rsdto = new ResultDto();
		//获取查询条件
		String pk_user = reqDto.getPk_user();
		if(StringUtils.isEmpty(pk_user)){
			rsdto.setStatus(1);
			rsdto.setMessage("参数用户为空!");
		}else{
			List<KmSettingQueryPoJo> listvo = kmSettingEntityDao.querySettingByUser(pk_user);
			rsdto.setStatus(0);
			rsdto.setMessage("数据查询成功!");
			rsdto.setNum(listvo==null?0:listvo.size());
			rsdto.setData(listvo);
		}
		
		return rsdto;
	}
	/**
	 * 生成用户设置信息
	 */
	@Transactional
	public ResultDto insertUserSetting(List<KmSettingEntity> listvo) {
		ResultDto  rsDto = new ResultDto();
		rsDto.setStatus(0);
		rsDto.setMessage("添加完成!");
		if(listvo!=null && listvo.size()>0){
			//首先删除该用户下所有已经设置的信息
			String pk_user = listvo.get(0).getPk_user();
			kmSettingEntityDao.deleteByCondition(" pk_user='"+pk_user+"'");
			for(KmSettingEntity vo : listvo){
				vo.setPk_setting(IDFactory.getInstance().getID());
				vo.setDtcreate(new Date());
				vo.setDr(0);
				vo.setTs(DateUtil.formatDateTime(new Date()));
				int i_add = kmSettingEntityDao.insertSelective(vo);
			}
		}
		
		return rsDto;
	}
	/**
	 * 查询用户所有行业信息-带是否设置信息
	 */
	public ResultDto queryAllIndustryByUser(RequestDto reqDto) {
		ResultDto  rsDto = new ResultDto();
		String pk_user = reqDto.getPk_user();
		if(StringUtils.isEmpty(pk_user)){
			rsDto.setStatus(1);
			rsDto.setMessage("未获取用户信息!");
		}else{
			List<UserIndustryPojo> listvo = kmSettingEntityDao.queryAllIndustryByUser(pk_user);
			rsDto.setStatus(0);
			rsDto.setCount(listvo==null?0:listvo.size());
			rsDto.setNum(listvo==null?0:listvo.size());
			rsDto.setMessage("数据查询成功!");
			rsDto.setData(listvo);
		}
		return rsDto;
	}

}
